public with sharing class WBCommentController {
        
        
    /**
     *  Create Lead by weibo info
     */
    public PageReference  CreateLead(){
    	
    	String retUrl = ApexPages.currentPage().getParameters().get('retUrl');
    	String Wbid = ApexPages.currentPage().getParameters().get('wbid');
    	
    	WBContent__c WBContent = new WBContent__c();
    	
		//system.debug('@@@@@@@@@@@@@@@@@@@@@@@@@@ lead.Id : '+lead.Id);
		
		if ( Wbid!=null && Wbid.length()>0 ){
			WBContent = [SELECT  Id,text__c, Create_Date__c, created_at__c, 
						  Keyword__c, Keyword_Category__c, Profile_Image__c, 
						  source__c, Stage__c,  uid__c, user__c,  truncated__c, mid__c, 
						  Weibo_URL__c
					FROM WBContent__c Where id=:Wbid ];
		}
    	
    	if( WBContent!=null && WBContent.Id!=null ){
	    	// cerate Lead
	    	Lead lead = new Lead();
	        lead.LastName = WBContent.user__c;
	        lead.Description__c = WBContent.text__c;
	        lead.UID__c = WBContent.uid__c;
	        lead.Create_At__c = WBContent.created_at__c;
	        lead.Source__c = WBContent.source__c;
	        lead.Company = 'Unknown';
	        lead.M_ID__c = WBContent.mid__c;
	        Database.insert( lead );
	    	//system.debug('@@@@@@@@@@@@@@@@@@@@@@@@@@ lead.Id : '+lead.Id);
	    	if( lead.Id != null ){
	    		WBContent.Lead__c = lead.Id;
	    		update WBContent;
	    	}
    	}
    	
    	PageReference toPage = null;
    	if( retUrl!=null && retUrl.length()>0 ){
    		toPage = new PageReference( retUrl );
    	}
    	
    	return toPage;
    }
    
    /**
     *  Create Case by weibo info
     */
    public PageReference CreateCase(){
    	
    	String retUrl = ApexPages.currentPage().getParameters().get('retUrl');
    	String Wbid = ApexPages.currentPage().getParameters().get('wbid');
    	
    	WBContent__c WBContent = new WBContent__c();
					
		if ( Wbid!=null && Wbid.length()>0 ){
			WBContent = [SELECT  Id,text__c, Create_Date__c, created_at__c, 
						  Keyword__c, Keyword_Category__c, Profile_Image__c, 
						  source__c, Stage__c,  uid__c, user__c,  truncated__c, mid__c 
					FROM WBContent__c Where id=:Wbid ];
		}
    	
    	if( WBContent!=null && WBContent.Id!=null ){
    		// cerate Lead
    		Case cas = new Case();
    	
	        cas.Description__c = WBContent.text__c;
	        cas.Weibo_User__c = WBContent.user__c;
	        cas.Weibo_No__c = WBContent.mid__c;
	        cas.Source__c =  WBContent.source__c;
	        //
	        Database.insert( cas );
	        
	        if( cas.Id != null ){
	    		WBContent.Case__c = cas.Id;
	    		update WBContent;
	    	}
    	}
    	
    	PageReference toPage = null;
    	if( retUrl!=null && retUrl.length()>0 ){
    		toPage = new PageReference( retUrl );
    	}
    	
    	return toPage;
    }
    
    /**
     *  Refrensh Weibo Info
     */
     public PageReference RefreshWeiboInfo(){
     	String retUrl = ApexPages.currentPage().getParameters().get('retUrl');
    	String Wbid = ApexPages.currentPage().getParameters().get('wbid');
     	
     	
     	WB_OpenApi.WB_WeiboInfo_Show_In WB_by_id = new WB_OpenApi.WB_WeiboInfo_Show_In();
     	WB_by_id.id  = Wbid;
     	String jsonResult = WB_by_id.toRequestUrl();
     	
     	//if( jsonResult.contains( comment.text__c) ){
            //comment.Status__c = 'sent';
           // WBContent__c id = System.JSON.serialize( jsonResult ,WBContent__c );
            //WBContent__c WBcotent = [SELECT ID FROM WBContent__c WHERE ID=:Wbid];
            
        //}else{
            //comment.Status__c = 'fail';
        //}
     	
     	return new PageReference( retUrl );
     }
     
    /**
     * Reply to Weibo
     */ 
    public void replyWeibo(){
    	
    	String paramWbid = ApexPages.currentPage().getParameters().get('wbid');
    	String paramRecdId=ApexPages.currentPage().getParameters().get('recordid');
    	String content =  ApexPages.currentPage().getParameters().get('content');
    	
        WBComment__c comment = new WBComment__c();
        comment.text__c = content;
        comment.mid__c = paramWbid;
        comment.source__c ='Salesforce';
         
        String jsonResult = '';
        system.debug('@@@@@@@@@@@@@@@@@@@@@@@@@@  ID :  '+ comment.mid__c);
        system.debug('@@@@@@@@@@@@@@@@@@@@@@@@@@  CN :  '+ content); 
         
        if( comment.mid__c!=null && comment.mid__c.length()>10 ){
                
            
            WB_OpenApi.WB_Comment_Create_In wbc_in = new WB_OpenApi.WB_Comment_Create_In();
            wbc_in.id = comment.mid__c;
            wbc_in.comment = comment.text__c;
            jsonResult = wbc_in.toRequestURL();
            
            system.debug('@@@@@@@@@@@@@@@@@@@@@@@@@@  :  '+ content);
            system.debug('@@@@@@@@@@@@@@@@@@@@@@@@@@  :  '+ jsonResult);
        }
        
        if( jsonResult.contains( comment.text__c) ){
            //comment.Status__c = 'sent';
        }else{
            //comment.Status__c = 'fail';
        }
        insert comment;
                
    }
     
    /**
     *  Show All Comments
     */
    public String jsonResult {get;set;}
    // get Sina Comments By MID
    public List<WB_OpenApi.WB_Show_Comments_ById_Out> getWeiboCommentsByID(){
        
        String PARAM_MID = ApexPages.currentPage().getParameters().get('PARAM_MID');
        
        List<WB_OpenApi.WB_Show_Comments_ById_Out> listComments = new List<WB_OpenApi.WB_Show_Comments_ById_Out>();
        system.debug('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@'+ PARAM_MID );
        if( PARAM_MID !=null  ){
        
            try{
                WB_OpenApi.WB_Show_Comments_ById_In wb_in = new WB_OpenApi.WB_Show_Comments_ById_In(); 
                wb_in.id = PARAM_MID ;
                String req = WB_OpenApi.url_show_comments_byId;
               
                string jsonResult = wb_in.toRequestUrl();
                
                if(jsonResult.indexOf('error',0) == -1){
                    // get response content
                    WB_OpenApi.WB_Show_Comments_ById_Out_Array wb_out = new WB_OpenApi.WB_Show_Comments_ById_Out_Array();
                    WB_OpenApi.WB_Show_Comments_ById_Out_Array arr = wb_out.parse(jsonResult);
                    if( arr.comments!=null ){
                                listComments = arr.comments;
                    }
                }else{
                        // fail
                }
                
            }
            catch(Exception e){
                 ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.ERROR, e.getMessage()));
            }
        }
        return listComments;
    }

}